import React, { useState, useEffect } from 'react';
import { Tree, Button, Card, message, Space } from 'antd';
import { useParams, useNavigate } from 'react-router-dom';
import { getRoleById } from '@/api';

const RolesPermissions = () => {
    const { id } = useParams();
    const navigate = useNavigate();
    const [role, setRole] = useState(null);
    const [checkedKeys, setCheckedKeys] = useState([]);

    // 权限树数据结构
    const treeData = [
        {
            title: '权限管理',
            key: 'acl',
            children: [
                {
                    title: '用户管理',
                    key: 'user',
                    children: [
                        { title: '添加', key: 'user.add' },
                        { title: '修改', key: 'user.edit' },
                        { title: '删除', key: 'user.delete' },
                        { title: '分配角色', key: 'user.assign' },
                        { title: '查看信息', key: 'user.view' },
                    ]
                },
                {
                    title: '角色管理',
                    key: 'role',
                    children: [
                        { title: '添加', key: 'role.add' },
                        { title: '修改', key: 'role.edit' },
                        { title: '删除', key: 'role.delete' },
                        { title: '分配权限', key: 'role.assign' },
                    ]
                },
                {
                    title: '权限管理',
                    key: 'permission',
                    children: [
                        { title: '添加', key: 'permission.add' },
                        { title: '修改', key: 'permission.edit' },
                        { title: '删除', key: 'permission.delete' },
                    ]
                }
            ]
        },
        {
            title: '基础数据',
            key: 'base',
            children: [
                {
                    title: '数据字典',
                    key: 'dict',
                    children: [
                        { title: '修改', key: 'dict.edit' },
                        { title: '添加', key: 'dict.add' },
                    ]
                },
                {
                    title: '货主管理',
                    key: 'owner',
                },
                {
                    title: '货品管理',
                    key: 'goods',
                },
                {
                    title: '货品分类',
                    key: 'category',
                }
            ]
        },
        {
            title: '仓位管理',
            key: 'warehouse',
            children: [
                { title: '仓库管理', key: 'warehouse.store' },
                { title: '入库管理', key: 'warehouse.in' },
                { title: '库内管理', key: 'warehouse.internal' },
                { title: '出库管理', key: 'warehouse.out' },
                { title: '配置管理', key: 'warehouse.config' },
            ]
        }
    ];

    // 获取角色信息
    const fetchRoleInfo = async () => {
        try {
            const data = await getRoleById(id);
            setRole(data);
            // 设置初始选中的权限
            setCheckedKeys(data.permissions || []);
        } catch (error) {
            message.error('获取角色信息失败');
        }
    };

    useEffect(() => {
        fetchRoleInfo();
    }, [id]);

    // 处理权限变更
    const onCheck = (checkedKeysValue) => {
        setCheckedKeys(checkedKeysValue);
    };

    // 保存权限设置
    const handleSave = async () => {
        message.success('权限分配成功');
        navigate(-1);
    };

    return (
        <Card
            title={`分配权限: ${role?.name || ''}`}
            extra={
                <Space>
                    <Button onClick={() => navigate(-1)}>
                        返回
                    </Button>
                    <Button type="primary" onClick={handleSave}>
                        保存
                    </Button>
                </Space>
            }
        >
            <Tree
                checkable
                defaultExpandAll
                checkedKeys={checkedKeys}
                onCheck={onCheck}
                treeData={treeData}
            />
        </Card>
    );
};

export default RolesPermissions;